cmake_minimum_required(VERSION 3.13.4)

message(STATUS "--- Enter ${CMAKE_CURRENT_SOURCE_DIR}")
string(REGEX REPLACE ".*/" "" CURRENT_DIR_NAME "${CMAKE_CURRENT_SOURCE_DIR}")

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  if(POLICY CMP0091)
    cmake_policy(SET CMP0091 NEW)
  endif(POLICY CMP0091)
endif()


# #################################################################
# ## Project
# #################################################################
set(PROJECT_NAME "${CURRENT_DIR_NAME}")
project(${PROJECT_NAME})

# #################################################################
# ## Add target
# #################################################################
file(GLOB_RECURSE SOURCE_FILES "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp" "${CMAKE_CURRENT_LIST_DIR}/src/*.c")
file(GLOB_RECURSE HEADER_FILES "${CMAKE_CURRENT_LIST_DIR}/include/*.h")
message(STATUS "SOURCE_FILES = ${SOURCE_FILES}")
message(STATUS "HEADER_FILES = ${HEADER_FILES}")

add_executable(${PROJECT_NAME} "${SOURCE_FILES}")
include_directories("${CMAKE_CURRENT_LIST_DIR}/include")

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  # Windows-specific configuration

  # MultiThreaded:            对应MT
  # MultiThreadedDLL:         对应MD
  # MultiThreadedDebug:       对应MTd
  # MultiThreadedDebugDLL:    对应MDd
  # 它默认值为MultiThreaded$<$<CONFIG:Debug>:Debug>DLL.
  string(TOLOWER "${CMAKE_BUILD_TYPE}" cmake_build_type_lower)
  if ("${cmake_build_type_lower}" STREQUAL "release")
    set_property(TARGET ${PROJECT_NAME} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
  elseif ("${cmake_build_type_lower}" STREQUAL "debug")
    set_property(TARGET ${PROJECT_NAME} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreadedDebug")
  else()
    message(FATAL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}")
  endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    # Linux-specific configuration
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
    # macOS-specific configuration
else()
    # Default configuration for other platforms
endif()

# #################################################################
# ## Installing
# #################################################################
install(
  TARGETS ${PROJECT_NAME}
  RUNTIME DESTINATION bin
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION libstatic
)